package com.jasonzhou.onetomanycontrol.base

import android.app.Application
import android.content.*
import android.content.res.Configuration
import android.os.IBinder
import android.preference.PreferenceManager
import com.jasonzhou.onetomanycontrol.constant.GlobalData
import com.jasonzhou.onetomanycontrol.constant.MessageConstant
import com.jasonzhou.onetomanycontrol.service.SCCCommunication
import com.jasonzhou.onetomanycontrol.stack.ActivityStackManager
import com.jasonzhou.onetomanycontrol.util.AppLanguageUtils
import com.jasonzhou.onetomanycontrol.util.CustomToast

class AppContent : Application() {
    lateinit var dataCtrl: GlobalData
    lateinit var toast: CustomToast
    lateinit var sccCommunication: SCCCommunication

    private var language = ""

    companion object {
        lateinit var instance: AppContent
        const val APP_ID = "b9ec248aa2"
        const val APP_KEY = "667e8b0d-87de-43fe-914b-0d51dfa8d398"
    }

    private lateinit var preferences: SharedPreferences



    private val bluetoothConnect = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            sccCommunication = (service as SCCCommunication.CommBinder).communicationService
            sccCommunication.initialize(this@AppContent)
        }

        override fun onServiceDisconnected(name: ComponentName) {}
    }


    override fun onCreate() {
        super.onCreate()
        instance = this
        registerActivityLifecycleCallbacks(ActivityStackManager.getInstance())

//        CrashReport.initCrashReport(this, APP_ID, true);
        toast = CustomToast(this)
        dataCtrl = GlobalData()
        bindService(Intent(this, SCCCommunication::class.java), bluetoothConnect, Context.BIND_AUTO_CREATE)

    }

    override fun attachBaseContext(base: Context) {
        preferences = PreferenceManager.getDefaultSharedPreferences(base)
        super.attachBaseContext(AppLanguageUtils.attachBaseContext(base, getAppLanguage()))
    }


    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        onLanguageChange()
    }

    private fun onLanguageChange() {
        AppLanguageUtils.changeAppLanguage(this, AppLanguageUtils.getSupportLanguage(language))
    }

    private fun getAppLanguage() = getString(MessageConstant.KEY_LANGUAGE_VALUE, MessageConstant.AUTO).apply { language = this }

    /**
     * 存储数据
     *
     * @param key
     * @param o
     */
    fun saveData(key: String, o: Any) {
        val editor = preferences.edit()
        when (o) {
            is Int -> editor.putInt(key, o)
            is String -> editor.putString(key, o)
            is Long -> editor.putLong(key, o)
            is Boolean -> editor.putBoolean(key, o)
            is Float -> editor.putFloat(key, o)
            else -> editor.putString(key, o.toString())
        }
        editor.apply()
    }

    /**
     * remove data
     *
     * @param key
     */
    fun removeData(key: String) {
        val editor = preferences.edit()
        editor.remove(key).apply()
    }

    fun getString(key: String, defValue: String = "") = preferences.getString(key, defValue) ?: defValue

    fun getBoolean(key: String, defValue: Boolean = false) = preferences.getBoolean(key, defValue)

    fun getInt(key: String, defValue: Int = 0) = preferences.getInt(key, defValue)

    fun getFloat(key: String, defValue: Int = 0) = preferences.getFloat(key, defValue.toFloat())

    fun getLong(key: String, defValue: Long = 0) = preferences.getLong(key, defValue)

    fun isHas(key: String) = preferences.contains(key)

}
